// 提示
const _toast = (msg) => {
    if (msg === "") return;
    msg = `${msg}`;
    // #ifdef APP-PLUS
    plus.nativeUI.toast(msg);
    // #endif
    // #ifndef APP-PLUS
    uni.showToast({
        title: msg,
        icon: "none",
        duration: 1500,
        mask: false
    });
    // #endif
}
// 提示框
const _modal = (content) => {
    if (content === "") return;
    content = `${content}`;
    return new Promise((resolve, reject) => {
        uni.showModal({
            icon: "none",
            duration: 1500,
            mask: true,
            title: '提示',
            showCancel: false,
            content: content,
            success(res) {
                if (res.confirm) {
                    resolve()
                } else if (res.cancel) {
                    reject()
                }
            }
        })
    })
}
// 加载
const _loading = (msg = "正在加载...") => {
    uni.showLoading({
        title: msg,
        mask: true
    });
}
// 页面跳转
const _toView = (nav) => {
    uni.navigateTo({
        url: `/pages/${nav}`,
        animationType: "pop-in",
        animationDuration: 300
    });
}
// 页面重定向
const _toRedirect = (nav) => {
    uni.redirectTo({
        url: `/pages/${nav}`,
        animationType: "pop-in",
        animationDuration: 300
    });
}
// 页面重定向
const _toSwitchTab = (nav) => {
    uni.switchTab({
        url: `/pages/${nav}`,
        animationType: "pop-in",
        animationDuration: 300
    });
}
// 页面后退
const _toBack = (delta = 1) => {
    uni.navigateBack({
        delta,
        animationType: "pop-out",
        animationDuration: 300
    });
}
// 复制
const _copy = (data, msg = "复制成功") => {
    if (data && typeof data === "string") {
        uni.setClipboardData({
            data,
            success: () => {
                uni.hideToast();
                _toast(msg);
            }
        });
    }
}
// 获取网络状态
const _getNetworkType = () => {
    uni.getNetworkType({
        success: (res) => res.networkType
    });
}
// 预览图片
const _previewImage = (dataList = [], current = 0) => {
    uni.previewImage({
        current: current,
        urls: dataList,
        indicator: "number",
        loop: true
    });
}
// 拨打电话
const _makePhoneCall = (phone = "") => {
    if (phone) {
        uni.makePhoneCall({
            phoneNumber: phone
        })
    }
}

const _host = 'http://www.lanjingwp.com';
const _ws_host = 'ws://www.lanjingwp.com:8282';

// const _host = 'https://wst.zhitongda.cn/svc';
// const _ws_host = 'https://wst.zhitongda.cn/svc:8080'
const _ajax = (params, mask) => {
    if (mask) {
        _loading();
    }
    let token = uni.getStorageSync("token") || "";
    params.data = params.data || {};
    params.data.token = token;
    if(params.url.substr(0,4) != 'http') {
        params.url = _host + params.url;
    }
    uni.request({
        url: params.url,
        method: params.method || "POST",
        data: params.data,
        complete: (res) => {
            // console.log(params.url);
            // console.log(JSON.stringify(params.data));
            // console.log(JSON.stringify(res));
            if (mask) {
                uni.hideLoading();
            }
            if (res && res.statusCode == 200) {
                if (typeof params.success == 'function') {
                    params.success(res.data);
                }
            }else if(res.statusCode == 401) {
                var pages = getCurrentPages();
                var page = pages[pages.length - 1];
                if(page && page.route !== 'pages/login/login') {
                    uni.clearStorageSync();
                    uni.reLaunch({
                        url: '/pages/login/login'
                    });
                }
            }
        }
    });
}

const _upload = (params) => {
    _loading("努力上传中");
    if(params.url.substr(0,4) != 'http') {
        params.url = _host + params.url;
    }
    uni.uploadFile({
        url: params.url,
        filePath: params.filePath,
        name: params.name || 'file',
        formData: params.formData,
        complete: (res) => {
            uni.hideLoading();
            console.log(JSON.stringify(res));
            if(res && res.statusCode == 200) {
                if (typeof params.success == 'function') {
                    params.success(JSON.parse(res.data));
                }
            }
        }
    });
}

const _toUrl = (url) => {
    if(url.substr(0,4) != 'http') {
        url = _host + url;
    }
    console.log(url);
    //#ifndef APP-PLUS
    uni.navigateTo({
        url: '/pages/web/web?url=' + encodeURIComponent(url)
    });
    //#endif
    //#ifdef APP-PLUS
    plus.runtime.openWeb(url);
    //#endif
}

export default {
    _toast,
    _modal,
    _loading,
    _toView,
    _toRedirect,
    _toSwitchTab,
    _toBack,
    _copy,
    _getNetworkType,
    _previewImage,
    _makePhoneCall,
    _ajax,
    _upload,
    _host,
    _ws_host,
    _toUrl
}

